// releasePage/pages/announcePublish/editor/editor.js
const app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        keyHeight: 0,
        keyShow: false,
        isIphone: false,
        formats: {},
        ipxBottom: '',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let _that = this;
        if (app.globalData.isIpx) {
            this.setData({
                ipxBottom: 34,
            });
        }
        wx.getSystemInfo({
            success: function (res) {
                let isIpx;
                //model中包含着设备信息
                var model = res.model;
                if (model.search('iPhone') != -1) {
                    isIpx = true;
                } else {
                    isIpx = false;
                }
                _that.setData({
                    isIphone: isIpx,
                });
            },
        });
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    /** editor 部分 **/
    getEditorValue(e) {
        this.setData({
            ['formData.content']: e.detail.html,
        });
    },
    onEditorReady() {
        const _that = this;
        wx.createSelectorQuery()
            .select('#editor')
            .context(function (res) {
                _that.editorCtx = res.context;
                wx.showLoading({
                    title: '加载内容中...',
                });
                wx.hideLoading();
                let pages = getCurrentPages(); //当前页面栈
                let prevPage = pages[pages.length - 2]; //上一页面
                _that.editorCtx.setContents({
                    html: prevPage.data.htmlContent,
                    success: (res) => {
                        console.log(res);
                    },
                    fail: (res) => {
                        console.log(res);
                    },
                });
            })
            .exec();
    },
    insertDivider() {
        this.editorCtx.insertDivider({
            success: function () {
                console.log('insert divider success');
            },
        });
    },
    format(e) {
        let { name, value } = e.target.dataset;
        if (!name) return;
        // console.log('format', name, value)
        this.editorCtx.format(name, value);
    },
    insertImage() {
        var that = this;
        wx.chooseImage({
            count: 1,
            success(res) {
                wx.showLoading({
                    title: '上传中...',
                });
                // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
                const tempFilePaths = res.tempFilePaths;
                //执行上传文件操作
                wx.uploadFile({
                    url: 'https://www.map8.com/static/uploadFiles',
                    name: 'files',
                    formData: {
                        savetype: '03',
                        quality: '',
                    },
                    comefrom: '05',
                    filePath: tempFilePaths[0],
                    success(res) {
                        const data = JSON.parse(res.data); //获取到的json 转成数组格式 进行赋值 和渲染图片
                        // console.log(data);
                        console.log(data.urls);
                        that.editorCtx.insertImage({
                            src: data.urls,
                            data: {
                                id: 'abcd',
                                role: 'god',
                            },
                            success: function () {
                                console.log('insert image success');
                                wx.hideLoading();
                            },
                        });
                    },
                    fail(e) {
                        wx.hideLoading();
                        console.log(e);
                    },
                    complete(e) {
                        wx.hideLoading();
                        console.log(e);
                    },
                });
            },
        });
    },
    undoBtn(e) {
        this.editorCtx.undo({
            success: function () {
                console.log('undosuccess');
            },
        });
    },
    redoBtn(e) {
        this.editorCtx.redo({
            success: function () {
                console.log('redosuccess');
            },
        });
    },
    onStatusChange(e) {
        const formats = e.detail;
        this.setData({
            formats,
        });
    },
    /** editor 部分结束 **/
    editorFocus(e) {
        this.setData(
            {
                keyShow: true,
            },
            () => {
                wx.onKeyboardHeightChange((res) => {
                    console.log(res.height);
                    this.setData({
                        keyHeight: res.height,
                    });
                });
            }
        );
    },
    editorBlur(e) {
        this.setData({
            keyHeight: 0,
            keyShow: false,
        });
    },
    // 提交
    submit(e) {
        let _that = this;
        let htmlContent;
        _that.editorCtx.getContents({
            success: function (res) {
                let htmlNone = res.html.replace(/wx:nodeid="\d+"/g, '');
                // console.log(htmlNone)
                if (htmlNone == '<p ><br ></p>' || htmlNone == '<p><br></p>') {
                    htmlContent = '';
                } else {
                    htmlContent = res.html.replace(
                        /\<img/gi,
                        '<img class="rich-text-img"'
                    );
                }
                console.log(htmlContent);
                let pages = getCurrentPages(); //当前页面栈
                let prevPage = pages[pages.length - 2]; //上一页面
                prevPage.setData({
                    htmlContent: htmlContent,
                });
                wx.navigateBack({
                    detal: 1,
                });
            },
        });

        // setTimeout(function() {
        //     wx.navigateBack({
        //         delta: 1 // 返回上一级页面。
        //     });
        // }, 2000);
    },
});
